package com.example.mapster;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Friends extends Activity {

	ArrayList<Friend> fL;
	ArrayList<String> names;
	Hashtable<String, Integer> ids;
	View listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friends);

		populateFriends();

		/*
		 * listView = (ListView) findViewById(R.id.friendsList);
		 * registerForContextMenu(listView);
		 */
		
		findViewById(R.id.back).setOnClickListener(
				new View.OnClickListener() {
					public void onClick(View v) {
						finish();
					}
				});
		
		findViewById(R.id.requestFriend).setOnClickListener(
				new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(Friends.this,
								Request_Friend.class);
						startActivity(intent);
					}
				});

		findViewById(R.id.viewRequests).setOnClickListener(
				new View.OnClickListener() {
					public void onClick(View v) {
						filterFriendRequests();
					}
				});
		findViewById(R.id.viewFriends).setOnClickListener(
				new View.OnClickListener() {
					public void onClick(View v) {
						filterFriends();
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_friends, menu);
		return true;
	}

	/*
	 * @Override public void onCreateContextMenu(ContextMenu menu, View v,
	 * ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo);
	 * menu.setHeaderTitle(getString(R.string.menu_context_title)); MenuInflater
	 * inflater = getMenuInflater(); inflater.inflate(R.menu.your_context_menu,
	 * menu); }
	 */

	public void populateFriends() {
		Database db = new Database(this);
		db.open();
		ListView lv = (ListView) findViewById(R.id.friendsList);
		ids = new Hashtable<String, Integer>();
		fL = new ArrayList<Friend>();
		names = new ArrayList<String>();

		fL = db.GetAllFriends();

		for (int i = 0; i < fL.size(); i++) {
			String name = fL.get(i).CloudUsername;
			if (!fL.get(i).Approved) {
				name = name + " - Request";
			}else{
				name = name+" - "+fL.get(i).CurrentLocation;
			}
			ids.put(name, fL.get(i).CloudId);
			names.add(name);
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, names);
		lv.setAdapter(adapter);

		lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View v,
					int index, long arg3) {
				// TODO Auto-generated method stub
				final String friend = ((TextView) v).getText().toString();
				// String str=lv.getItemAtPosition(index).toString();
				System.out.println("Item at: " + friend);

				if (friend.contains("Request")) {
					final String opts[] = { "Accept", "Deny" };

					AlertDialog.Builder ab = new AlertDialog.Builder(
							Friends.this);
					ab.setTitle("Friend Request");
					ab.setItems(opts, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface d, int choice) {
							if (choice == 0) {
								Toast.makeText(Friends.this, "Added",
										Toast.LENGTH_SHORT).show();
								AcceptRequest task = new AcceptRequest();
								task.execute(friend);
							} else if (choice == 1) {
								Toast.makeText(Friends.this, "Denied",
										Toast.LENGTH_SHORT).show();
								DeleteFriend task = new DeleteFriend();
								task.execute(friend);
							}
						}
					});
					ab.show();
				} else {
					final String opts[] = { "Yes", "No" };

					AlertDialog.Builder ab = new AlertDialog.Builder(
							Friends.this);
					ab.setTitle("Delete?");
					ab.setItems(opts, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface d, int choice) {
							if (choice == 0) {
								Toast.makeText(Friends.this, "Deleted",
										Toast.LENGTH_SHORT).show();
								DeleteFriend task = new DeleteFriend();
								task.execute(friend);
							} else if (choice == 1) {

							}
						}
					});
					ab.show();
				}
				return true;
			}

		});
	}

	public void filterFriendRequests() {
		names = new ArrayList<String>();

		for (int i = 0; i < fL.size(); i++) {
			if (!fL.get(i).Approved) {
				String name = fL.get(i).CloudUsername;
				name = name + " - Request";
				names.add(name);
			}
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, names);
		ListView lv = (ListView) findViewById(R.id.friendsList);
		lv.setAdapter(adapter);
	}

	public void filterFriends() {
		names = new ArrayList<String>();

		for (int i = 0; i < fL.size(); i++) {
			if (fL.get(i).Approved) {
				String name = fL.get(i).CloudUsername+" - "+fL.get(i).CurrentLocation;
				names.add(name);
			}
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, names);
		ListView lv = (ListView) findViewById(R.id.friendsList);
		lv.setAdapter(adapter);
	}

	/*****************
	 * 
	 * Async Tasks
	 * 
	 *****************/

	public class AcceptRequest extends AsyncTask<String, Void, Friend[]> {
		// perform the database access
		User user;

		@Override
		protected Friend[] doInBackground(String... params) {
			new CloudHelper().AcceptFriendRequest(ids.get(params[0]));
			return new CloudHelper().GetAllFriends(MainActivity.getID(),
					Friends.this);
		} // end method doInBackground

		@Override
		protected void onPostExecute(Friend[] f) {
			populateFriends();
		} // end method onPostExecute
	}
	
	public class DeleteFriend extends AsyncTask<String, Void, Friend[]> {
		// perform the database access
		User user;

		@Override
		protected Friend[] doInBackground(String... params) {
			new CloudHelper().DeleteFriend(ids.get(params[0]));
			return new CloudHelper().GetAllFriends(MainActivity.getID(),
					Friends.this);
		} // end method doInBackground

		@Override
		protected void onPostExecute(Friend[] f) {
			populateFriends();
		} // end method onPostExecute
	}

}

/*
 * FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
 * fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
 */
